#!/bin/bash

####################
## 主机初始化脚本 ##
####################

# 设置时区
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 系统更新
#yum -y update 

# 同步时间
sudo yum -y install ntpdate && ntpdate pool.ntp.org 
sudo echo "1 1 * * * /usr/sbin/ntpdate pool.ntp.org" >> /var/spool/cron/root
sudo ntpdate ntp1.aliyun.com 
# 设置本地Docker镜像私有仓库解析
sudo echo -e  "请输入本地Docker镜像私有库内网IP(一般和jenkins所在服务器ip一致)"
sudo read  STORAGE_HOST

tmp=`cat /etc/hosts | grep registry.docker.hc  | wc -l`
if [[ $tmp > 0 ]]; then
	sed -i 's/.* registry.docker.hc/${STORAGE_HOST} registry.docker.hc /' /etc/hosts
else
	echo ${STORAGE_HOST} registry.docker.hc >> /etc/hosts
fi

tmp=`cat /etc/hosts | grep nfs.docker.hc  | wc -l`
if [[ $tmp > 0 ]]; then
	sed -i 's/.* nfs.docker.hc/${STORAGE_HOST} nfs.docker.hc /' /etc/hosts
else
	echo ${STORAGE_HOST} nfs.docker.hc >> /etc/hosts
fi

# 禁用SELINUX
sudo setenforce 0
sudo sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

# 禁用防火墙
sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service

# 安装Docker
tar -zxvf docker-20.10.7.tgz
mv docker/* /usr/bin/
sudo mkdir -p /usr/lib/systemd/system/
sudo tee /usr/lib/systemd/system/docker.service <<-'EOF'
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
EOF

# Docker配置                                       
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://45p7qdl5.mirror.aliyuncs.com"],
  "insecure-registries":["registry.docker.hc:5000"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m",
    "max-file": "3"
  },
  "graph":"/hc/dockerlib/docker"
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl enable docker

# 安装NFS
sudo yum install -y nfs-utils

sudo yum install -y system-storage-manager container-storage-setup

# 设置定时清理脚本
sudo mkdir -p /hc/script/
sudo tee /hc/script/clear.sh <<-'EOF'
#!/bin/bash
#删除所有<none>镜像
sudo docker rmi $(docker images -f "dangling=true" -q)
EOF
sudo chmod +x /hc/script/clear.sh
sudo echo "2 1 * * * sh /hc/script/clear.sh" >> /var/spool/cron/root



sudo tee /hc/script/clearspringboot.sh <<-'EOF'
#!/bin/bash

# 获取所有镜像
IMAGES=$(docker images --format '{{.Repository}}={{.Tag}}')
# 遍历所有镜像
for IMAGE in $IMAGES; do
# 获取镜像名称和标签
    REPO=$(echo $IMAGE | cut -d= -f1)
    TAG=$(echo $IMAGE | cut -d= -f2)
    echo "${REPO}  ${TAG}"
# 获取镜像的所有版本
    IMAGE_IDS=$(docker images $REPO --format '{{.ID}}\t{{.CreatedAt}}' | sort -k2 | awk '{print $1}')
    echo "IMAGE_IDS  ${IMAGE_IDS}"
# 计算要保留的版本数
    KEEP_NUM=2
# 删除所有除最新 $KEEP_NUM 个版本之外的版本
    DELETE_IDS=$(echo $IMAGE_IDS | awk -v keep=$KEEP_NUM '{for(i=1; i<=NF-keep; i++) print $i}')
    echo "DELETE_IDS  ${DELETE_IDS}"
    for DELETE_ID in $DELETE_IDS; do
        docker rmi -f $DELETE_ID
        echo "删除  ${REPO}  ${TAG}   ${DELETE_ID}"
    done
done

EOF

sudo chmod +x /hc/script/clearspringboot.sh
sudo echo "2 2 * * * sh /hc/script/clearspringboot.sh" >> /var/spool/cron/root



# 开启crond服务
sudo systemctl start crond
sudo systemctl enable crond


# 安装GIT
sudo yum install -y git
sudo yum install -y unzip
sudo yum install -y vim

sudo mkdir -p /hc/data/
sudo chmod -R 777 /hc/data
export HISTTIMEFORMAT="%F %T "



#安全删除
cp safe-rm /usr/local/bin/
#替换rm命令
sudo echo 'alias rm="safe-rm -i"' >> /etc/bashrc
#刷新修改的文件
source /etc/bashrc
#创建链接,将safe-rm替换rm
ln -s /usr/local/bin/safe-rm /usr/local/bin/rm
sudo chmod -R 777 /usr/local/bin/safe-rm 
echo 'PATH=/usr/local/bin:$PATH' >>  /etc/profile
source /etc/profile
sudo tee /etc/safe-rm.conf <<-'EOF'
/
/*
/etc
/etc/*
/data
/data/mysql
/data/mysql/datadir
/data/mysql/datadir/*
/usr
/usr/local
/usr/local/bin
/usr/local/bin/*
/hc
/hc/data/nfs
/hc/data/nfs/*
/home/hc/data/nfs
/home/hc/data/nfs/*
/home/hc/
/home/hc/*
/hc/*
/bin/*
/boot/*
/dev/*
/home/*
/lib/*
/lib64/*
/media/*
/mnt/*
/opt/*
/proc/*
/root/*
/run/*
/sbin/*
/srv/*
/sys/*
/var/*
EOF



# TODO 时区设置失败
# TODO 安装完成校验
# TODO 定期清理日志 镜像
